<?
//------------------------------------------------------------------------------
//
//  [ Comments blablabla ]
//
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------


function get_users_comments($uid, $count=10)
{
   if( !($uid>0 && $count>0) ) return 'can`t get user`s comments!';
   
   $q = 'select * from `comments` where `user_id`=?d and `isfirst`=0 order by `date` desc limit ?d';
   
   $db = mysql_init();
   
   
   $comments = $db->select($q, $uid, $count);
   
   return $comments;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------


function add_comment($comment)   // very trustful function, eh... |
{                                // use it only via next function v
   $db = mysql_init();

   $q = 'insert into `comments` (?#) values(?a)';

   $nid = $db->query($q, array_keys($comment), array_values($comment));

   return $nid;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------


function add_new_comment($comment, $files=array())
{
   if( !is_array($comment) || !isset($comment['bug_id']) ) return false;
   
   $ncid = add_comment($comment);

   upload_files($ncid, $files);

   return $ncid;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------


function edit_comment($comment, $files)
{
   if( !is_array($comment) || !isset($comment['bug_id']) ) return false;
   
   $cid = $comment['id'];
   unset($comment['id']);

   $db = mysql_init();

   $q = 'update `comments` set ?a where `id`=?d';

   $db->query($q, $comment, $cid);

   upload_files($cid, $files);

   return true;

}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

function get_first_comment($bid)
{
   if( !($bid>0) ) return false;

   $q = 'select * from `comments` where `bug_id`=?d and `isfirst`=1 limit 1';

   $db = mysql_init();

   $cmt= $db->selectRow($q, $bid);

   return $cmt;
}

//------------------------------------------------------------------------------

function get_bug_comments($bid, $currentpage=0)
{
   if( !($bid>0) ) return false;
   
   global $_x_counter;
   $offset = _comments_on_page * $currentpage;

   $q = 'select * from `comments` where `bug_id`=?d and `isfirst`=0 order by `date` limit ?d, ?d';

   $db = mysql_init();


   $cmts = $db->selectPage($_x_counter, $q, $bid, $offset, _comments_on_page);

   return $cmts;
}

function count_bug_comments($bid)
{
   if( !($bid>0) ) return 0;
   
   $q = 'select COUNT(`id`) as `c` from `comments` where `bug_id`=?d and `isfirst`=0';

   $db = mysql_init();

   $count = $db->selectRow($q, $bid);

   return $count['c'];
}

//------------------------------------------------------------------------------


function get_comment($cid)
{
   if( !($cid>0) ) return false;

   $q = 'select * from `comments` where `id`=?d limit 1';

   $db = mysql_init();

   $cmt = $db->selectRow($q, $cid);

   return $cmt;
}

//------------------------------------------------------------------------------


function deletecomment($cid)
{
   if( !($cid>0) ) return false;

   $q = 'delete from `comments` where `id`=?d limit 1';

   $db = mysql_init();

   $db->query($q, $cid);
   
   deletefiles($cid);

   return true;
}

//------------------------------------------------------------------------------


function deleteBugComments($bid)
{
   if( !($bid>0) ) return false;
   
   $comments   = get_bug_comments($bid);
   $comments[] = get_first_comment($bid);
   
   $c = count($comments);

   if($c>0)
   {
      for($i=0; $i<$c; $i++)
      {
         deletefiles($comments[$i]['id']);
      }
      
      $q = 'delete from `comments` where `bug_id`=?d';

      $db = mysql_init();

      $db->query($q, $bid);
   }

   return true;
}

//------------------------------------------------------------------------------


function is_comment($cid, $comment=false)
{
   if( !($cid>0) ) return false;
   if(!is_array($comment))
   {
      $q = 'select * from `comments` where `id`=?d limit 1';

      $db = mysql_init();

      $comment = $db->selectRow($q, $cid);
   }

   if(isset($comment['id']) && $comment['id'] == $cid) return $comment;
   else return false;

}

//------------------------------------------------------------------------------


function is_his_comment($cid, $comment=false, $uid)
{
   $comment = is_comment($cid, $comment);

   if(isset($comment['user_id']) && $comment['user_id'] == $uid) return true;
   else return false;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

function html_bug_comment($comment, $counter, $user, $caneditall)
{
   if( !isset($comment['id']) ) return '<!-- no such comment('.$comment['id'].') -->';
   
   //---------------------------------------------------------------------------
   
   $canedit = ( $caneditall ||
                ( is_his_comment($comment['id'], $comment, $user['id']) && check_user_right('bugs', 'edit_own',  $user['rights']) )
              );

   $editlnk = '&nbsp;';
   if( $canedit )
   {
      $tpl = get_template('bug_page_comment_editlnk.html');
      $kwd = array('_CID_', '_UID_', '_BID_');
      
      $editlnk = str_replace($kwd, array($comment['id'], $user['id'], $comment['bug_id']), $tpl);
   }
   
   //---------------------------------------------------------------------------
   
   $candelete = check_user_right('bugs', 'delete_comments',  $user['rights']);
   
   $candelete = ( ($candelete && $caneditall) ||
                  (is_his_comment($comment['id'], $comment, $user['id']) && $candelete && $canedit )
                );
                
   $deletelnk = '&nbsp;';
   
   if( $candelete )
   {
      $tpl = get_template('bug_page_comment_deletelnk.html');
      $kwd = array('_CID_', '_UID_', '_BID_');

      $deletelnk = str_replace($kwd, array($comment['id'], $user['id'], $comment['bug_id']), $tpl);
   }
                
   //---------------------------------------------------------------------------
   
   $tpl = get_template('bug_page_comment.html');
   $kwd = array(  '_CMTID_',
                  '_COUNTER_',
                  '_AUTHORCOMMENT_',
                  '_IP_',
                  '_LOGIN_',
                  '_NAME_',
                  '_CTYPE_',
                  '_TEXT_',
                  '_DATE_',
                  '_EDITLNK_',
                  '_DELETELNK_',
                  '_FILES_');


   $author = get_user($comment['user_id']);
   
   $uname = ($comment['user_id']==_anonymous_id) ? $comment['user_name'] : $author['name'];
   $uname = ($uname) ? $uname : $comment['user_name'];

   
   $r = array( $comment['id'],
               $counter,
               $author['comment'],
               $comment['ip'],
               $author['login'],
               $uname,
               trim_spaces_rfc( get_rfc_item_name('ctypes', $comment['ctype_id']) ),
               $comment['text'],
               $comment['date'],
               $editlnk,
               $deletelnk,
               html_attachlist($comment['id'])
             );

   return str_replace($kwd, $r, $tpl);
}

//------------------------------------------------------------------------------


function html_bug_comments($comments, $page, $user, $pager)
{
   $c = count($comments);
   
   if( !$c>0 || !isset($comments[0]['id']) ) return '';
   
   
   $caneditall = check_user_right('bugs', 'edit_other',  $user['rights']);
   
   $html = '';
   
   
   for($i=0; $i<$c; $i++)
   {
      $counter = $page * _comments_on_page + $i + 1;
      $html .= html_bug_comment($comments[$i], $counter, $user, $caneditall);
   }
   
   
   $tpl = get_template('bug_page_comments.html');
   $kwd = array('_PAGER_', '_COMMENTS_');
   
   $html = str_replace($kwd, array($pager, $html), $tpl);
   
   return $html;
}

//------------------------------------------------------------------------------


function comments_pager($all, $current, $onpage)
{
   return '';
}

//------------------------------------------------------------------------------


function html_edit_comment($user, $comment)
{
   if( !isset($comment['id']) ) return '';

   $tpl = get_template('bug_page_comment_form.html');
   $kws = array(  '_BID_',
                  '_CID_',
                  '_UID_',
                  '_USERNAME_',
                  '_COMMENT_',
                  '_ATTACHMENTS_',
                  '_CTYPES_'
               );
               
   $author = ($comment['id'] > 0 && $comment['user_id']!=_anonymous_id) ? get_user($comment['user_id']) : $user;
   $uname  = ($comment['id'] > 0 && $comment['user_id']==_anonymous_id) ? $comment['user_name'] : $author['name'];
   
   $ctypes = list_ref_opts('ctypes' , $comment['ctype_id']);
               
   $r = array( $comment['bug_id'],
               $comment['id'],
               $author['id'],
               $uname,
               $comment['text'],
               html_attachlist($comment['id'], true),
               $ctypes
             );

   return str_replace($kws, $r, $tpl);
}


//------------------------------------------------------------------------------





















?>
